package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.pojo.*;
import com.neuedu.pojo.TNurse;
import com.neuedu.mapper.TNurseMapper;
import com.neuedu.pojo.dto.nurse.NurseInfoDTO;
import com.neuedu.service.ITAccountService;
import com.neuedu.service.ITNurseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-16
 */
@Service
public class TNurseServiceImpl extends ServiceImpl<TNurseMapper, TNurse> implements ITNurseService {
    @Autowired
    TNurseMapper nurseMapper;
    @Autowired
    private ITAccountService itAccountService;

    @Override
    public int add(TNurse nurse) {

        return nurseMapper.insert(nurse);
    }

    @Override
    public Result<List<NurseInfoDTO>> getPage(Integer currentPage, Integer pageSize, String nurseName, Integer bindingState, Integer nurseWorkstate) {
        Page<TNurse> page = new Page<>(currentPage, pageSize);
        // 设置条件查询的构造器
        LambdaQueryWrapper<TNurse> wrapper = new LambdaQueryWrapper<>();
        // 若条件为null，这个相当于全部查询
        wrapper.like(StringUtils.isNotBlank(nurseName), TNurse::getNurseName, nurseName)
                .eq(bindingState != null, TNurse::getBindingState, bindingState)
                .eq(nurseWorkstate != null, TNurse::getNurseWorkstate, nurseWorkstate);
        nurseMapper.selectPage(page, wrapper);
        List<TNurse> records = page.getRecords();
        //查询t_account,封装nursePageDTOList
        List<NurseInfoDTO> nurseInfoDTOList = records.stream().map(tNurse -> {
            NurseInfoDTO nurseInfoDTO = new NurseInfoDTO();
            BeanUtils.copyProperties(tNurse, nurseInfoDTO);
            TAccount one = itAccountService.getOne(new LambdaQueryWrapper<TAccount>()
                    .eq(TAccount::getAccUsername, tNurse.getNurseNumber()));
            nurseInfoDTO.setPassword(one.getAccPassword());
            nurseInfoDTO.setAccId(one.getAccId());
            return nurseInfoDTO;
        }).collect(Collectors.toList());
        return Result.success(nurseInfoDTOList, "查询成功", page.getTotal());
    }

    @Override
    public Result findBynurseNumber(int nurseNumber) {
        LambdaQueryWrapper<TNurse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TNurse::getNurseNumber, nurseNumber);
        List<TNurse> nurses = nurseMapper.selectList(lqw);
        return Result.success(nurses, "查询成功");
    }

    @Override
    public Result findAll() {
        List<TNurse> nurses = nurseMapper.selectList(null);
        return Result.success(nurses);
    }

    @Override
    public Result modBindingStateById(Integer nurseId) {
        LambdaUpdateWrapper<TNurse> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TNurse::getNurseId,nurseId)
                .set(TNurse::getBindingState,1);
        return Result.success(nurseMapper.update(null,wrapper),"修改成功");
    }
}
